#!/bin/bash
current_path=$(
  cd "$(dirname "$0")" || exit
  pwd
)
echo "$current_path"
user_name=$(echo "$USER")
userName=$(echo "$USER")

currentPath=/home/$userName/Podman
function Install() {

  sudo mkdir -p "$currentPath"
  cd "$currentPath" || exit
  sudo mkdir -p mongo/logs
  sudo mkdir -p mongo/data
  sudo mkdir -p nginx/logs
  sudo mkdir -p nginx/html
  sudo mkdir -p nginx/conf.d
  sudo mkdir -p redis/data
  sudo mkdir -p influxdb 
  sudo chmod -R 777 "$currentPath"

  echo "
    protected-mode no
    port 6380
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    dir ./
    appendonly no
    " >$currentPath/redis/redis.conf

  sudo chmod -R 777 /home/$userName
  echo "server {
        listen       80;
        listen  [::]:80;
        server_name  localhost;
         location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
    " >$currentPath/nginx/conf.d/default.conf

  sudo apt install podman -y

  sudo sh -c ' echo "
         unqualified-search-registries = [\"docker.io\"]
         [[registry]]
         prefix = \"docker.io\"
         location =\"docker.mirrors.ustc.edu.cn\"
         insecure = true
         [[registry.mirror]]
         location = \"hub-mirror.c.163.com\"
         insecure = true
         [[registry.mirror]]
         location = \"registry.docker-cn.com\"
         insecure = true
      " > /etc/containers/registries.conf' && sudo systemctl daemon-reload && sudo systemctl restart podman.service

sudo sh -c ' echo "
        unqualified-search-registries = [\"docker.io\"]
        [[registry]]
        prefix = \"docker.io\"
        location = \"ekcolff1.mirror.aliyuncs.com\"
      " > /etc/containers/registries.conf' && sudo systemctl daemon-reload && sudo systemctl restart podman.service
}

function PullImage() {
  sudo podman pull docker.io/library/mongo
  sudo podman pull docker.io/library/redis
  sudo podman pull docker.io/library/mysql
  sudo podman pull docker.io/library/nginx
  sudo podman pull docker.io/redislabs/rejson
  sudo podman pull docker.io/bitnami/etcd
#  sudo podman pull docker.dragonflydb.io/dragonflydb/dragonfly
#  sudo podman pull docker.io/dph5199278/dragonflydb
  sudo podman pull docker.io/nsqio/nsq
  sudo podman pull influxdb
}
function CreateContains() {
  sudo podman stop $(sudo podman ps -a -q) && sudo podman rm -f $(sudo podman ps -a -q)
  sudo podman run -itd -p 80:80 --name nginx01 \
    -v $currentPath/nginx/conf.d:/etc/nginx/conf.d \
    -v $currentPath/nginx/html:/usr/share/nginx/html \
    -v $currentPath/nginx/logs:/var/log/nginx \
    library/nginx

  sudo podman run -itd -p 3306:3306 --name mysql01 -e MYSQL_ROOT_PASSWORD=root \
    library/mysql

  sudo podman run -itd -p 27017:27017 --name mongo01 \
    -e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=root \
    library/mongo

  sudo podman run -itd -p 6380:6380 --name redis01 \
    -v $currentPath/redis/redis.conf:/etc/redis/redis.conf \
    -v $currentPath/redis/data:/data \
    library/redis \
    redis-server /etc/redis/redis.conf --appendonly yes


#  sudo podman run -itd -p 6379:6379 --name dragonflydb01 --ulimit memlock=-1 docker.dragonflydb.io/dragonflydb/dragonfly

  #      docker run -itd -p 6379:6379 --name dragonfly01 --network=host --ulimit memlock=-1 docker.dragonflydb.io/dragonflydb/dragonfly

  #      docker  run -itd --name etcd01 -p 2379:2379 -p 2380:2380  --alisten-client-urls http://0.0.0.0:2379 --advertise-client-urls http://0.0.0.0:2379

  sudo podman run -d -p 8086:8086 --name=influxdb01 -v /home/debi/Podman/influxdb:/var/lib/influxdb influxdb

sudo podman network create app-tier --driver bridge

  sudo podman run -d --name etcd-server \
    --network app-tier \
    --publish 2379:2379 \
    --publish 2380:2380 \
    --env ALLOW_NONE_AUTHENTICATION=yes \
    --env ETCD_ADVERTISE_CLIENT_URLS=http://etcd-server:2379 \
    bitnami/etcd:latest
#
#  #             name: 节点名称
#  #             advertise-client-urls: 知客户端url, 也就是服务的url
#  #             initial-advertise-peer-urls: 告知集群其他节点url
#  #             listen-peer-urls: 监听URL，用于与其他节点通讯
#  #             initial-cluster-token: 集群的ID
#  #             initial-cluster: 集群中所有节点


#  sudo podman run -it --rm \
#    --network app-tier \
#    --env ALLOW_NONE_AUTHENTICATION=yes \
#    bitnami/etcd:latest etcdctl --endpoints http://etcd-server:2379 put /message Hello

  #      docker run -itd --name nsq-lookupd \
  #            -p 4160:4160 -p 4161:4161 nsqio/nsq /nsqlookupd
  #
  sudo podman run -itd --name nsq-d \
    -p 4150:4150 -p 4151:4151 \
    nsqio/nsq /nsqd \
    --broadcast-address=192.168.20.9

  sudo podman run -d \
    -p 3000:3000 \
    --name=grafana01 \
    -e "GF_INSTALL_PLUGINS=grafana-clock-panel,grafana-simple-json-datasource" \
    grafana/grafana-enterprise
  #
  #      sudo  podman run -d -p 2379:2379 -p 2380:2380  appcelerator/etcd --listen-client-urls http://0.0.0.0:2379 --advertise-client-urls http://0.0.0.0:2379

}
function ChangeIpAddr() {

  read -p "if change Ip addr 192.168.20.? [1<?<20] (0|1 exit) :" ipAddr
  case $ipAddr in
  [0-1])
    echo "Exit!"
    ;;
  *)
    echo "address 192.168.20.$ipAddr!"
    sudo echo "
     source /etc/network/interfaces.d/*
auto lo
iface lo inet loopback
auto ens33
allow-hotplug ens33
#iface ens33 inet dhcp
iface ens33 inet static
            address 192.168.20.$ipAddr
            netmask 255.255.255.0
            gateway 192.168.20.2
        " >/etc/network/interfaces && systemctl restart networking.service
    ;;
  esac

}

read -p "if Install Docker[y/n]:" isInstall

if [[ "[yY]" =~ $isInstall ]]; then
  Install
fi

read -p "if Install PullImage[y/n]:" pullImage

if [[ "[yY]" =~ $pullImage ]]; then
  PullImage
fi

read -p "if createContains? [y/n] " createC

if [[ "[yY]" =~ $createC ]]; then
  CreateContains
fi
read -p "if changeIdAddr? [y/n] " changeIdAddr

if [[ "[yY]" =~ $changeIdAddr ]]; then
  ChangeIpAddr
fi

#sudo podman start $(sudo podman ps -a -q)

#need test
#vim /etc/profile
#sudo sh -c ' echo "
#export https_proxy=http://172.24.127.113:7890
#export http_proxy=http://172.24.127.113:7890
#export all_proxy=socks5://172.24.127.113:7890
#" >> /etc/profile ' && source /etc/profile
#
#curl -0 https://github.com/dragonflydb/dragonfly/releases/download/v0.15.0/dragonfly-x86_64.tar.gz
#
#wget -e "http_proxy=http://172.24.127.113:7890" https://github.com/dragonflydb/dragonfly/releases/download/v0.15.0/dragonfly-x86_64.tar.gz
#
#sudo podman pull bitnami/etcd
#quay.io/coreos/etcd:v2.3.8
#
#docker run -d -v /usr/share/ca-certificates/:/etc/ssl/certs -p 4001:4001 -p 2380:2380 -p 2379:2379 \
#  --name etcd bitnami/etcd \
#  -name etcd0 \
#  -advertise-client-urls http://${HostIP}:2379,http://${HostIP}:4001 \
#  -listen-client-urls http://0.0.0.0:2379,http://0.0.0.0:4001 \
#  -initial-advertise-peer-urls http://${HostIP}:2380 \
#  -listen-peer-urls http://0.0.0.0:2380 \
#  -initial-cluster-token etcd-cluster-1 \
#  -initial-cluster etcd0=http://${HostIP}:2380 \
#  -initial-cluster-state new
